package br.great.DAO;

/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
import java.sql.*;

/**
 * @author Developpement of PhotoMap
 */
public class PgConnect {

    private static volatile PgConnect instacePgConnect = new PgConnect();
    String sDBDriver = "org.postgresql.Driver";
    String sConnStr = "jdbc:postgresql://localhost/photomapLIG?user=photomap&password=photomap";
    Connection conn = null;
    ResultSet rs = null;

    /** The constructor of the class PgConnect
     */
    private PgConnect() {
        try {
            Class.forName(sDBDriver);
        } catch (java.lang.ClassNotFoundException e) {
            System.err.println("conn(): " + e.getMessage());
        }
    }

    public static PgConnect getInstance() {
        return instacePgConnect;
    }

    /** the action of insert for the script of SQL
     * @param String sql
     * @exception SQLException ex
     */
    public void executeInsert(String sql) throws SQLException {
      
            conn = DriverManager.getConnection(sConnStr);
            Statement stmt = conn.createStatement();
            int n = stmt.executeUpdate(sql);      
    }
    public PreparedStatement executeInsertWithPreparedStatement(String statement) throws SQLException{
        return conn.prepareStatement(statement);
    }

    /** execute the action for the script of SQL
     * @param String sql
     * @exception SQLException ex
     * @return ResultSet rs
     */
    public ResultSet executeQuery(String sql) {
        try {
            conn = DriverManager.getConnection(sConnStr);
            Statement stmt;
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
        } catch (SQLException ex) {
            System.err.println("conn.executeQuery: " + ex.getMessage() + " with the SQL:" + sql);
        }
        return rs;
    }

    /** execute the action for the script of SQL
     * @param String sql
     * @exception SQLException ex
     */
    public void executeDelete(String sql) {
        try {
            conn = DriverManager.getConnection(sConnStr);
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            System.err.println("conn.executeDelete: " + ex.getMessage());
        }
    }

    /** execute the action of update for the script of SQL
     * @param String sql
     * @exception SQLException ex
     * @return int n
     */
    public int executeUpdate(String sql) {
        int n = 0;
        try {
            conn = DriverManager.getConnection(sConnStr);
            Statement stmt = conn.createStatement();

            n = stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            System.err.println("conn.executeUpdate: " + ex.getMessage() + " with the SQL:" + sql);
        }
        return n;
    }

    /** close the connect of the database
     * @param String sql
     * @exception SQLException ex
     */
    public void Close() {
        /*try {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }            
        } catch (SQLException ex) {
            System.err.println("conn.executeClose: " + ex.getMessage());
        }
    }*/
    }
    public void closeAll() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }            
        } catch (SQLException ex) {
            System.err.println("conn.executeClose: " + ex.getMessage());
        }    
    }
}
